package com.atguigu.blog.controller;

import com.atguigu.blog.entity.User;
import com.atguigu.blog.service.UserService;
import com.atguigu.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 2.1 用户登录
     * @author Petrichor
     * @date 2023/10/24 10:50
     **/
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User userDB = userService.findUserByUP(user);
        if (userDB == null){
            //没查到
            //用户名或密码错误
            return Result.build(null, 201, "用户名或密码错误");
        }else {
            //查得到 没有错误
            Map data = new HashMap();
            data.put("uid", userDB.getUid());
            data.put("name", userDB.getUsername());
            return Result.ok(data);
        }
    }
    /**
     * 5.1 用户信息展现
     * @author Petrichor
     * @date 2023/10/24 21:04
     **/
    @GetMapping("/findUserById/{uid}")
    public Result findUserById(@PathVariable Long uid){

        User user = userService.findUserById(uid);
        return Result.ok(user);
    }
    /**
     * 5.2 用户信息修改
     * @author Petrichor
     * @date 2023/10/25 08:44
     **/
    @PutMapping("/updateUserById")
    public Result updateUserById(@RequestBody User user){
        userService.updateUserById(user);
        return Result.ok();
    }
}
